package com.xiao.asset.controller;

import com.alibaba.fastjson.JSONObject;
import com.xiao.asset.bean.AssetBean;
import com.xiao.asset.bean.BumenBean;
import com.xiao.asset.bean.LeixingBean;
import com.xiao.asset.bean.YewuBean;
import com.xiao.asset.services.AssetService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 衰草寒烟 on 2017/2/10.
 */
@Controller("asset")
@RequestMapping("/asset")
public class AssetController {
    private static Logger log= Logger.getLogger(AssetController.class);
    @Autowired
    private AssetService assetservice;

    /**
     * 资产列表页面
     */
    @RequestMapping("/list")
    public ModelAndView getasset(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("assetlist");
        return mv;
    }
    /**
     * 资产添加页面
     */
    @RequestMapping("/add")
    public ModelAndView addym(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("add");
        return mv;
    }
    /**
     * 资产修改页面
     */
    @RequestMapping("/update")
    public ModelAndView update(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("update");
        return mv;
    }
    /**
     * 添加页面
     */
    @RequestMapping("/qtadd")
    public ModelAndView ywadd(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("qtadd");
        return mv;
    }
    /**
     * 资产添加表单
     */
    @RequestMapping("/assetadd")
    @ResponseBody
    public String assetadd(HttpServletRequest request){
        AssetBean ab=new AssetBean();
        ab.setMing(request.getParameter("ming"));
        ab.setBianhao(request.getParameter("bianhao"));
        ab.setSuoshuren(request.getParameter("suoshuren"));
        ab.setBumenid(assetservice.getbmbyming(request.getParameter("bumen")).getBumenid());
        ab.setLeixingid(assetservice.getlxbyming(request.getParameter("leixing")).getLeixingid());
        ab.setPeizhi(request.getParameter("peizhi"));
        SimpleDateFormat sf=new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
        Date shiyong=null;
        try {
            shiyong=sf.parse(request.getParameter("shiyong"));
        } catch (ParseException e) {
            log.warn("资产添加时间解析出错！");
            e.printStackTrace();
        }
        ab.setMaishijian(shiyong);
        ab.setChushizhi(request.getParameter("chushizhi"));
        ab.setShuxing(request.getParameter("shuxing"));
        ab.setDidian(request.getParameter("didian"));
        ab.setWeizhi(request.getParameter("weizhi"));
        ab.setBeizhu(request.getParameter("beizhu"));
        String rs = assetservice.add(ab);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }

    /**
     * 业务添加
     */
    @RequestMapping("/yewuadd")
    @ResponseBody
    public String yewuadd(HttpServletRequest request){
        YewuBean yw=new YewuBean();
        yw.setMing(request.getParameter("ming"));
        String rs = assetservice.ywdd(yw);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }
    /**
     * 部门添加
     */
    @RequestMapping("/bumenadd")
    @ResponseBody
    public String bumenadd(HttpServletRequest request){
        BumenBean bm=new BumenBean();
        bm.setMing(request.getParameter("ming"));
        String rs = assetservice.bmadd(bm);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }
    /**
     * 类型添加
     */
    @RequestMapping("/leixingadd")
    @ResponseBody
    public String leixingadd(HttpServletRequest request){
        LeixingBean lb=new LeixingBean();
        lb.setMing(request.getParameter("ming"));
        String rs = assetservice.lxadd(lb);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }
    /**
     * 资产删除
     */
    @RequestMapping("/del")
    public String update(@RequestParam(value = "assetid")int assetidid){
        String rs = assetservice.del(assetidid);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }
    /**
     * 类型删除
     */
    @RequestMapping("/dellx")
    public String lxdel(@RequestParam(value = "leixingid")int leixingid){
        String rs = assetservice.dellx(leixingid);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }
    /**
     * 部门删除
     */
    @RequestMapping("/delbm")
    public String delbm(@RequestParam(value = "bumenid")int bumenid){
        String rs = assetservice.delbm(bumenid);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }
    /**
     * 业务删除
     */
    @RequestMapping("/delyw")
    public String delyw(@RequestParam(value = "yewuid")int yewuid){
        String rs = assetservice.delyw(yewuid);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }
    /**
     * 某资产从某业务系统中的移除（该资产不删除）
     */
    public String ywzcdel(String ywming,String bianhao){
        String rs = assetservice.ywzcdel(ywming, bianhao);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }
    /**
     * 资产修改表单
     */
    @RequestMapping("/edit")
    @ResponseBody
    public String assetupdate(HttpServletRequest request){
        AssetBean ab=new AssetBean();
        ab.setMing(request.getParameter("ming"));
        ab.setBianhao(request.getParameter("bianhao"));
        ab.setSuoshuren(request.getParameter("suoshuren"));
        ab.setBumenid(assetservice.getbmbyming(request.getParameter("bumen")).getBumenid());
        ab.setLeixingid(assetservice.getlxbyming(request.getParameter("leixing")).getLeixingid());
        ab.setPeizhi(request.getParameter("peizhi"));
        SimpleDateFormat sf=new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
        Date shiyong=null;
        try {
            shiyong=sf.parse(request.getParameter("shiyong"));
        } catch (ParseException e) {
            log.warn("资产修改时间解析出错！");
            e.printStackTrace();
        }
        ab.setMaishijian(shiyong);
        ab.setChushizhi(request.getParameter("chushizhi"));
        ab.setShuxing(request.getParameter("shuxing"));
        ab.setDidian(request.getParameter("didian"));
        ab.setWeizhi(request.getParameter("weizhi"));
        ab.setBeizhu(request.getParameter("beizhu"));
        String rs = assetservice.update(ab);
        JSONObject js=new JSONObject();
        js.put("msg",rs);
        return js.toJSONString();
    }
    /**
     *demo
     */
    @RequestMapping("/getjson")
    @ResponseBody
    public String getjson(){
        String js= "[{\"id\":1,\"text\":\"Root node\",\"children\":[{\"id\":2,\"text\":\"Child node 1\"},{\"id\":3,\"text\":\"Child node 2\"}]}]";
        return js;
    }
}
